﻿<%@ WebHandler Language="C#" Class="WindowsPhoneSelectHandler" %>

using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using Newtonsoft.Json;

public class WindowsPhoneSelectHandler : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        string ActionSwitch = "";
        ActionSwitch = context.Request.QueryString["ActionSwitch"];
        string strConn = global::LinkToDatabase.strConn;
        DataSet ds = new DataSet();
        string strCmd;
        
        switch(ActionSwitch)
        {
            case "LoadArticleInfo":
                using (SqlConnection conn = new SqlConnection(strConn))
                {
                    strCmd = "select unboxing_article_id,unboxing_article_title,unboxing_article_publish_date from unboxing_article where unboxing_visible=1 order by unboxing_article_publish_date DESC";
                    using (SqlDataAdapter da = new SqlDataAdapter(strCmd, conn))
                    {
                        da.Fill(ds, "ArticleInfo");
                    }
                }
            break;
            case "LoadArticle":
            using (SqlConnection conn = new SqlConnection(strConn))
            {
                strCmd = "select unboxing_article_title,unboxing_article_content,unboxing_article_publish_date from unboxing_article where unboxing_article_id=@Aid and unboxing_visible=1";
                using (SqlDataAdapter da = new SqlDataAdapter(strCmd, conn))
                {
                    da.SelectCommand.Parameters.AddWithValue("@Aid", context.Request.QueryString["Aid"]);
                    da.Fill(ds, "Article");
                }
            }
            break;
            default:
            return;
        }
        context.Response.ContentType = "application/json";
        context.Response.Write(JsonConvert.SerializeObject(ds));
        return;
        //context.Response.ContentType = "text/plain";
        //context.Response.Write("Hello World");
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}